package com.winning.vsts.merge;

import com.winning.vsts.merge.service.configuration.VstsContext;
import com.winning.vsts.merge.service.domain.VstsProject;
import com.winning.vsts.merge.service.domain.VstsUser;
import com.winning.vsts.merge.service.service.VstsClient;
import com.winning.vsts.merge.service.service.VstsProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 自动审批
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/18
 */
@EnableJpaRepositories(basePackages = "com.winning.vsts.merge.service.jpa")
@SpringBootApplication(scanBasePackages = "com.winning.vsts.merge.service")
public class AutoApprovalApplication implements ApplicationRunner {

    @Autowired
    private VstsProjectService vstsProjectService;

    @Autowired
    private VstsContext vstsContext;

    @Autowired
    private VstsClient vstsClient;

    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .main(AutoApprovalApplication.class)
            .sources(AutoApprovalApplication.class)
            .web(WebApplicationType.NONE)
            .build().run(args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {

        String projectName = "winning-bas-cis-outpatient-clinical";
        String pullRequestId = "134647";
        List<VstsProject> vstsProjects = vstsProjectService.queryVstsProject(projectName, null);
        VstsProject vstsProject = vstsProjects.get(0);
        RestTemplate restTemplate = vstsContext.restTemplate(vstsProject);
        VstsUser vstsUser = vstsContext.vstsUser(vstsProject);
        vstsClient.autoApprove(restTemplate, vstsProject, pullRequestId, vstsUser.getVstsId());

    }
}
